package com.ishitong.platform.utils.spring;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class ODContextLoaderListener implements ServletContextListener {
	private Log logger = LogFactory.getLog(this.getClass());
	private  WebApplicationContext webApplicationContext;
	public ODContextLoaderListener() {
	}

	public void contextInitialized(ServletContextEvent event) {
		PlatformStartLoad psl = new PlatformStartLoad();
		contextLoader = createContextLoader();
		webApplicationContext=contextLoader.initWebApplicationContext(event.getServletContext());
		event.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webApplicationContext);
		psl.loadFreesql();// 加载freesql
	}

	protected ContextLoader createContextLoader() {
		return new ODContextLoader();
	}

	public ContextLoader getContextLoader() {
		return contextLoader;
	}

	public void contextDestroyed(ServletContextEvent event) {
		if (contextLoader != null)
			contextLoader.closeWebApplicationContext(event.getServletContext());
	}

	private ContextLoader contextLoader;
}
